package com.alibaba.controller;

import com.alibaba.pojo.Dept;
import com.alibaba.pojo.Result;
import com.alibaba.service.DeptService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
public class DeptController {
    @Autowired
    private DeptService deptService;

    @GetMapping("/depts")
    public Result list(){
        List<Dept> list = deptService.list();
        return Result.success(list);
    }
    @PostMapping("/depts")
    public Result add(@RequestBody Dept dept){
        deptService.add(dept);
        return Result.success();
    }

    @DeleteMapping("/depts")
    //显示声明  required= true ，id必须要写
    public Result deleteById(@RequestParam(value = "id",required = true) Integer id){
        deptService.deleteById(id);
        return Result.success();
    }

    @GetMapping("/depts/{id}")
    public Result query(@PathVariable(value = "id",required = true) Integer id){
        Dept deptQuery = deptService.query(id);
        return Result.success(deptQuery);
    }

    @PutMapping("/depts")
    public Result update(@RequestBody Dept dept){
        deptService.update(dept);
        return Result.success();
    }
}
